package file;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collector;
import java.util.stream.Collectors;

/**
 * @author guoqf
 * @date 2021/5/28 14:19
 */
public class ListStreamTest {
    public static void main(String[] args) {
        testListToMapWithFieldList();
    }

    private static void testListToMapWithObjectList() {
        List<Student> students = Arrays.asList(new Student(1, "xiaoming", 10), new Student(2, "xiaohong", 11), new Student(3, "xiaoming", 11));
        Map<String, List<Student>> collect = students.stream().collect(Collectors.groupingBy(Student::getName));
        collect.forEach((name, group) -> System.out.println("name: " + name + ", group: " + group));
    }

    private static void testListToMapWithFieldList() {
        List<Student> students = Arrays.asList(new Student(1, "xiaoming", 10), new Student(2, "xiaohong", 11), new Student(3, "xiaoming", 11));
        Map<String, List<Integer>> collect =

                students.stream().collect(Collectors.groupingBy(Student::getName, Collector.<Student, List<Integer>>of(ArrayList::new, (ids, stu) -> ids.add(stu.getId()), (oldVals, newVals) -> newVals)));
        collect.forEach((name, group) -> System.out.println("name: " + name + ", group: " + group));
    }


    static class Student {
        private int id;

        private String name;

        private int age;

        public Student() {
        }

        public Student(int id, String name, int age) {
            this.id = id;
            this.name = name;
            this.age = age;
        }

        public int getId() {
            return id;
        }

        public void setId(int id) {
            this.id = id;
        }

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public int getAge() {
            return age;
        }

        public void setAge(int age) {
            this.age = age;
        }

        /**
         * Returns a string representation of the object. In general, the
         * {@code toString} method returns a string that
         * "textually represents" this object. The result should
         * be a concise but informative representation that is easy for a
         * person to read.
         * It is recommended that all subclasses override this method.
         * <p>
         * The {@code toString} method for class {@code Object}
         * returns a string consisting of the name of the class of which the
         * object is an instance, the at-sign character `{@code @}', and
         * the unsigned hexadecimal representation of the hash code of the
         * object. In other words, this method returns a string equal to the
         * value of:
         * <blockquote>
         * <pre>
         * getClass().getName() + '@' + Integer.toHexString(hashCode())
         * </pre></blockquote>
         *
         * @return a string representation of the object.
         */
        @Override
        public String toString() {
            return String.format("{id: %d, name: %s, age: %d}", id, name, age);
        }
    }
}
